package com.qf.lanzhou.shop.api.controller;

import com.qf.lanzhou.shop.api.service.HistoryService;
import com.qf.lanzhou.shop.pojo.entity.Goods;
import com.qf.lanzhou.shop.pojo.entity.User;
import com.qf.lanzhou.shop.pojo.vo.BaseRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author Nerly
 * @Date 2021/1/11 20:03
 * @JDK 1.8
 * @Version 1.0
 */
@RestController
@Api(tags = "足迹接口")
public class HistoryController extends BaseController {
    @Autowired
    private HistoryService historyService;

    @GetMapping("/getHistory")
    @ApiOperation(value = "根据用户id获取足迹")
    public BaseRespVO selectByUserId(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        System.out.println("Session:   " + user);
        List<Goods> goodsList = historyService.selectByUserId(user);
        BaseRespVO baseRespVO = convertSuccessResult(goodsList);
        return baseRespVO;
    }

    @PostMapping("/walk")
    @ApiOperation(value = "用户浏览商品产生足迹")
    @ApiImplicitParams({@ApiImplicitParam(name = "user", value = "用户"),
            @ApiImplicitParam(name = "goods", value = "商品")})
    public BaseRespVO walk(Goods goods, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (null == user) {
            return convertSuccessResult(1);
        }
        int walk = historyService.walk(user, goods);
        if (walk == 0) {
            BaseRespVO errorWalk = convertErrorResult("error");
            return errorWalk;
        }
        BaseRespVO successWalk = convertSuccessResult(walk);
        return successWalk;
    }

}
